import json
import unittest

import numpy as np
import pandas as pd

from autoTask.taobao.redis_key_mange import Xgfx
from autoTask.taobao.sycm.xgfx import CacheAction
from autoTask.taobao.sycm.xgfx.CacheAction import put_cache_by_df
from common.redis import redis_client
from common.redis.util import hs_set_value_convert, base_value_convert
from common.utils import FileUtil, ExcelUtil


class MyTestCase(unittest.TestCase):

    # def setUp(self):
    #     mock_file_path = FileUtil.get_project_root() + r"/taskTemplateExcel/dev/xgfx/炒锅列表.txt"
    #     self.mock_list = ExcelUtil.pd_paste_2_csv(
    #         FileUtil.read_file_contents(file_path=mock_file_path)[0]).values.tolist()

    def test_get_all(self):
        dict_ = CacheAction.get_all('30天')
        print(dict_)
        for item in dict_.items():
            print(item)

    def test_get_search_key_list(self):
        result = CacheAction.get_search_key_list("炒锅", "30天")
        print(result)
        # for item in result:
        #     print(item)

    def test_get_one(self):
        print(CacheAction.get_one("炒锅", "30天"))

    def test_(self):
        for item in self.mock_list:
            redis_client.h_set_put(Xgfx.key + "测试", item[0], item)
        dicts_ = redis_client.h_set_get(Xgfx.key + "测试", key_type=str, value_type=list)
        for item in dicts_:
            print(item)

    # def test_put_df_head(self):
    #     df = pd.DataFrame({
    #         'name': ['Tom', 'Jerry', 'Mike', 'Emily', 'Jessie'],
    #         'age': [25, 30, 28, 23, 27],
    #         'gender': ['M', 'M', 'M', 'F', 'F'],
    #         'city': ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen', 'Hangzhou'],
    #         'salary': np.random.randint(10000, 20000, 5)
    #     })
    #     put_cache_by_df_head(Xgfx.key + "测试", df)

    def test_get_df_head(self):
        df_head_list = redis_client.h_set_get(Xgfx.key + "测试:表头", key_type=str, value_type=list)[-1].get("head")
        print(df_head_list)

    def test_put_cache_by_df(self):
        df = pd.DataFrame({
                    'name': ['Tom', 'Jerry', 'Mike', 'Emily', 'Jessie'],
                    'age': [25, 30, 28, 23, 27],
                    'gender': ['M', 'M', 'M', 'F', 'F'],
                    'city': ['Beijing', 'Shanghai', 'Guangzhou', 'Shenzhen', 'Hangzhou'],
                    'salary': np.random.randint(10000, 20000, 5)
                })
        put_cache_by_df(search_key='蒸锅', date_cycle='30天', df=df)
        # pass


if __name__ == '__main__':
    unittest.main()
